package cn.sdut.thrift.client;

import cn.sdut.thrift.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * @author MaJiatao
 * @description: TODO
 * @created: 2019/10/22
 */
public class HelloClientDemo {
    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 8090;
    public static final int TIME_OUT = 30000;

    public void startClient(String userName) {
        TTransport tTransport = null;
        try {
            tTransport = new TSocket(SERVER_IP, SERVER_PORT, TIME_OUT);
            TBinaryProtocol tBinaryProtocol = new TBinaryProtocol(tTransport);
            HelloWorldService.Client client = new HelloWorldService.Client(tBinaryProtocol);
            tTransport.open();
            String result = client.sayHello(userName);
            System.out.println("thrift client result = " + result);

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (tTransport  != null) {
                tTransport.close();
            }
        }
    }

    public static void main(String[] args) {
        HelloClientDemo client = new HelloClientDemo();
        client.startClient("Michael");
    }
}
